/**
 * Created by pello  on 2016/11/25.
 * 创建 wtmapSetLocation 用户设置位置指令
 */
app.controller("wtmapSetLocationController", ["$rootScope", "$scope", "wtmapService", "$timeout", "toaster"
    , function($rootScope, $scope, wtmapService, $timeout, toaster){

        //初始化地图指令
        $scope.mapIntialed = function(option){

            var promise = wtmapService.intialMap(option.mapid, false, 0, $rootScope.arcgisConfig.lods); //同步调用，获取承诺接口
            promise.then(function(mapobj){

                $scope.mapcadObj = mapobj;
                $scope.mapcadObj.addDynamicMap($rootScope.arcgisConfig.baseMap);

                $scope.loadLayer($scope.option.layers);
                $scope.mapcadObj.zoomTobyXY(option.center.x, option.center.y, option.zoom);

            }, function(data){
                //调用承诺接口reject();
            });
        };
        //获取中心点坐标
        //格式：{"type":"point","x":155307.451,"y":139762.144,"spatialReference":{"wkid":102100,"latestWkid":3857}}
        $scope.getCenter = function(){
            // console.log("$scope.mapcadObj" ,new Date(),mapobj,$scope.mapcadObj);
            return $scope.mapcadObj.appMap.extent.getCenter();
        }
        /**
         * 设置多点定位
         * @param points eg:[{x:0,y:2},{x:0,y:2}]
         */
        $scope.setLocation = function(points){
            if(points){
                if(points.length === 0){

                    toaster.pop("warning", "提示", "未找到定位点！");
                }
                var _timeout = $timeout(function(){
                    if(!$scope.mapcadObj){
                        $timeout(function(){$scope.setLocation(points)}, 500);
                    } else {

                        $scope.mapcadObj.setMapExtendByPoints(points, 3, $rootScope.centerOptions)
                        $timeout.cancel(_timeout);
                    }
                }, 500);
            }
        };
        /**
         * loadLayer  加载图层
         * @param layers [{layerName:"",layerFilter:""}] 图层名称
         */
        $scope.loadLayer = function(layers){

            if(!$scope.mapcadObj){

                var _timeout = $timeout($scope.loadLayer(layers), 500);

            } else {
                $timeout.cancel(_timeout);
            }
            // const LAYERS = $rootScope.arcgisConfig.layerInfosObj;//总图层配置
            // var cur_layers = layerNames;// 要添加图层的信息
            var featurefayers = [];
            if(layers && layers.length > 0){

                layers.forEach(function(item, index){
                    var featurelayer = $scope.mapcadObj.addFeatureLayer(item.layerName, {
                        maxScale : 0,
                        minScale : 0,
                        showInfoTemplate : false,
                        definitionExpression : item.layerFilter
                    });
                    featurefayers.push({layerName : item, featurelayer : featurelayer});
                });

                // featurefayer.setVisibility(dataItem.visibility);

                //$scope.featurefayers.push(featurefayer);
                return featurefayers;
            }
            else {
                console.log("没有添加设施图层");
                return;
            }

        }
    }]);

app.directive("wtmapSetLocation", function(){
    return {
        restrict : "EA",
        replace : true,
        template : "<div>" +
        '<wt-map mapid="{{ option.mapid }}" height="option.height" showgdmap="option.showgdmap" map-intialed="mapIntialed(option)"></wt-map>' +
        "</div>",
        scope : {
            option : "=",
            position : "=" //[]

        },

        controller : "wtmapSetLocationController",
        //require : "wtmapSetLocationController",
        link : function(scope, element, attrs, controller){

            var defaltOption = {
                mapid : "setLoction",
                center : {x : 155307.451, y : 139762.144, lng : 0, lat : 0},
                zoom : 1,
                height : 700,
                showgdmap : false,
                layerNames : ["设备点"]//要添加的图层名称数组 对应配置 main.js里$rootScope.arcgisConfig.layerInfos
            };
            if(!scope.option){
                //设置默认参数
                scope.option = defaltOption;
            } else {
                scope.option = _.extend(defaltOption, scope.option);
                // console.log(scope.option);
            }
            //监视position定位点的变化
            scope.$watch("position", function(oldValue, newValue){

                scope.setLocation(scope.position);

            });
            scope.setLocation(scope.position);
        }

    };
});

